<?php

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, DELETE");
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Cache-Control,Authorization");
$checkLogin = false;
include_once '../huayan/ahlims.php';
$request = $_REQUEST;
$fzx_id = $request['fzx_id'] ? $request['fzx_id'] : 1;
$global = reload_global_inc($fzx_id);
//require_once INC_DIR . "cy_func.php";
//include './customer_api_func.php.php';
include 'customer_api_func.php';

$DB->query('set names utf8');
ini_set("display_errors", "On");
error_reporting(1);
if($_REQUEST['action']){
    $action = $_REQUEST['action'];
}else{
    $jsonData = file_get_contents("php://input");
    $jsonData = json_decode($jsonData , true);
    if (isset($jsonData['action'])){
        $action = $jsonData['action'];
    }
}
unset($_REQUEST['action']);
//获取页面格式
if($action === 'get'){
    $data['code'] = '200';
    $data['msg'] = 'success';
    $data['data'] = getTemplates();
    echo json_encode($data , JSON_UNESCAPED_UNICODE);
}

if ($action === 'getClentOnly') {
    $client_info = getClentOnly();
    if ($client_info) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $client_info;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"],JSON_UNESCAPED_UNICODE);
    }
}
//查询检测单位
if ($action === 'selectClent') {
    if($_REQUEST['client_name']) {

        $client_info = selectClent($_REQUEST['client_name']);
        if ($client_info) {
            $data['code'] = '200';
            $data['msg'] = 'success';
            $data['data'] = $client_info;
            echo json_encode($data , JSON_UNESCAPED_UNICODE);
        } else {
            echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
        }
    }else{
        echo json_encode(['code' => 404, 'msg' => "参数错误"], JSON_UNESCAPED_UNICODE);
    }
}

//获取检测单位列表
if($action === 'getClent') {
    $client_id = '';
    if(isset($_REQUEST['client_id'])) {
        $client_id = $_REQUEST['client_id'];
    }
    $clent = getClentOnly($client_id);
    if ($clent) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $clent;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}
//获取站点
if($action === 'getSites') {
    $site_id = '';
    if($_REQUEST['site_id']) {
        $site_id = $_REQUEST['site_id'];
    }
    $sites= getSites($site_id);
    if ($sites) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $sites;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}
//搜索站点
if ($action === 'selectSites') {
    if($_REQUEST['site_name']) {

        $sites_info = selectSites($_REQUEST['site_name']);
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $sites_info;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    }else{
        echo json_encode(['code' => 404, 'msg' => "参数错误"], JSON_UNESCAPED_UNICODE);
    }
}

//获取检测类型
if($action === 'getLeixing') {

    $leixings= getLeixings();
    if ($leixings) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $leixings;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}

//获取检测标准
if($action === 'getJcbz') {

    $jcbz= getJcbz();
    if ($jcbz) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $jcbz;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}
if($action === 'getXmmb') {

    $xmmb = getXmmb();
    if ($xmmb) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $xmmb;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}
//获取检测项目
if($action === 'getJcxm'){
//    var_dump(getJcxm());
    $id = '';
    $jcxm= getJcxm($id);
    if ($jcxm) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $jcxm;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}

//添加委托信息
if($action === 'addCustomer'){
//    $jsonData = file_get_contents("php://input");
//
//// 解析JSON数据
//    $data = json_decode($jsonData, true);
//    dd($data);die;

    $header = getallheaders();
    $token = $header['token'];
//
    if(!$header['token']){
        echo json_encode(['code' => 404, 'msg' => "缺少token"], JSON_UNESCAPED_UNICODE);
        die;
    }
    $openid = getWxUser($token);
//    $data = file_get_contents('a.json');
//    $a = json_decode($data , true);
    unset($jsonData['action']);
    $a['sub_action'] = '保存';
//    $path = $rootdir . '/customer_cy/111.log';
//    $lines = file_get_contents($path);
////    var_dump((array)json_decode($lines));die;
//    $file = fopen($path, 'a');
//    $text = date('Y-m-d H:i:s') . "   :";
//    $text .= json_encode($_REQUEST);
//    fwrite($file, $text);
//    fclose($file);die;
//    $openid = '1231231231231';
    $id = '';
    if($_REQUEST['id']){
        $id = $_REQUEST['id'];
    }
    $data = $_REQUEST['data'];
    $data = $a;
//    unset($data['action']);
//    $data = $_POST;
    $cust= addCustomer($data , $openid , $id);
//    var_dump($cust);
//    die;
    if ($cust) {
        $mes['code'] = '200';
        $mes['msg'] = '保存成功';
        if(isset($cust['customer_id'])){
            $mes['data']['customer_id'] = $cust['customer_id'];
        }
        echo json_encode($mes,JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "保存失败"], JSON_UNESCAPED_UNICODE);
    }
}
if($action === 'weixinLogin'){
    if($_REQUEST['code']){
        $openid = getWeixinOpenid($_REQUEST['code']);
        if(!$openid['code']){
            echo json_encode(['code' => 404, 'msg' => $openid['msg']], JSON_UNESCAPED_UNICODE);
            die;
        }
        $token = addWxUser($openid);
        if($token){
            $mes['code'] = '200';
            $mes['msg'] = '保存成功';
            $mes['data']['token'] = $token;
            echo json_encode($mes);
        }else{
            echo json_encode(['code' => 404, 'msg' => "保存出错"], JSON_UNESCAPED_UNICODE);
        }

    }else{
        echo json_encode(['code' => 404, 'msg' => "参数错误"], JSON_UNESCAPED_UNICODE);
    }
}
//获取历史记录
if($action === 'getHistory'){
//    var_dump(getJcxm());
    $status = '';
    if($_REQUEST['status']){
        $status = $_REQUEST['status'];
    }
    $header = getallheaders();
    if(!$header['token']){
        echo json_encode(['code' => 404, 'msg' => "缺少token"], JSON_UNESCAPED_UNICODE);
        die;
    }
    $token = $header['token'];
    $openid = getWxUser($token);
    $his= getHistory($openid , $status);
    if ($his) {
        $data['code'] = '200';
        $data['msg'] = 'success';
        $data['data'] = $his;
        echo json_encode($data , JSON_UNESCAPED_UNICODE);
    } else {
        echo json_encode(['code' => 404, 'msg' => "未找到"], JSON_UNESCAPED_UNICODE);
    }
}